package org.pinda.server;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.pinda.network.ConnectionType;
import org.pinda.network.PacketUtils;

public abstract class IServer extends Thread {
	
	private Logger logger;
	private ClientAccepter clientAccepter;
	private boolean keepRunning;
	
	public IServer(int port) throws IOException {
		super("ServerThread:" + port);
		logger = Loggers.createLogger(getLoggerName(), Level.ALL);
		clientAccepter = new ClientAccepter(this, port);
	}
	
	public abstract void doServerLogic();
	
	@Override
	public void run() {
		keepRunning = true;
		clientAccepter.start();
		while(keepRunning) {
			doServerLogic();
			PacketUtils.sleep(1);
		}
		clientAccepter.onShutdown();
		logger.fine("Server got shutdown");
	}
	
	public void stopServer() {
		keepRunning = false;
	}
	
	public abstract String getLoggerName();
	
	public abstract ConnectionType getServerType();
	
	public boolean keepRunning() {
		return keepRunning;
	}

}
